#include <ch9.h>
#include <Max3421e.h>
#include <Max3421e_constants.h>
#include <Max_LCD.h>
#include <Usb.h>

#include <AndroidAccessory.h>

AndroidAccessory acc("Manufacturer",
"Model",
"Description",
"1.0",
"DataSend.namespace",
"0000000012345678");

/* hook up potentiometer edges to 5v and ground. 
 Wiper arm to Analog in pin A0
 
 Should be good from there
 
 */


int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  // declare the ledPin as an OUTPUT:

  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  acc.powerOn();
}

void loop()
{
  byte msg[0]; // one byte
  byte msg2[0];

  sensorValue = analogRead(sensorPin);
  sensorValue = map(sensorValue, 0, 1023, 0, 125);

  if (acc.isConnected())
  {
    // is connected
    msg[0] = sensorValue;
        acc.write(msg, 1);  
    int len = acc.read(msg2, sizeof(msg2), 1); // read data into msg variable
    if (len > 0) {
      if (msg2[0] == 1){ // compare received data
        Serial.print("try to turn on");
        digitalWrite(ledPin,HIGH); // turn on light
      }
      else{
        digitalWrite(ledPin,LOW); // turn off light
        Serial.print("try to turn on");
      }
    }

    //Serial.println(sensorValue, HEX);

    //delay(50); 

  }
}


